// import config from '../config/index';
let startTimeStamp = 0
let endTimeStamp = 0
import config from '@/config/config.js';
export const request = (params) => {

	if (!params.hideLoading) {
		// 请求前的提示
		startTimeStamp = Date.parse(new Date());
		// setTimeout(() => {
		uni.showLoading({
			title: '加载中',
			mask: true
		});
		// },2000)
	}

	// 获取信息
	// const token = uni.getStorageSync('token') ||
	// 	'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImlhdCI6MTczNTI3NDI2MCwiZXhwIjoxNzM1MzYwNjYwfQ.P1OAzyD715QfkZ40HsWC9_BFYJDZMAdnHeHRjAa2loWLdSwI-Y2sFQHFH3YM3yaXcMjYLqjUuc56e9RnT1NVTA'

	const token = uni.getStorageSync('token')
	const header = {
		'Content-type': 'application/json',
	};
	if (token) {
		header['Authorization'] = 'Bearer ' + token
		console.log('token', token)
	}
	// 处理请求头
	params.header = params.header ? Object.assign(header, params.header) : header;
	const baseUrl = params.baseUrl ? params.baseUrl : config.baseUrl;
	// const baseUrl = 'http://kd248bd8.natappfree.cc';
	return new Promise((resolve, reject) => {
		uni.request({
			...params,
			url: baseUrl + params.url,
			success: (result) => {
				uni.hideLoading({
					noConflict: true
				});
				console.log('result', result)
				if (result.data.status == 401) {
					uni.showToast({
						title: '登录过期，请重新登录',
						icon: 'none'
					});
					uni.clearStorageSync()
					setTimeout(() => {
						uni.navigateTo({
							url: "/pagesExtra/loginA/loginA", // 登录页面路径
							success(res) {
								console.log('成功', res);
							},
							fail(err) {

								console.log('失败', err);
							}
						});
					}, 1500)
				} else {
					resolve(result.data);
				}
			},
			fail: (err) => {
				if (!params.hideLoading) {
					uni.hideLoading({
						noConflict: true
					});
					uni.showToast({
						title: '系统异常，请联系管理员',
						icon: 'none'
					});
				}
				reject(err);
			}
		});

	});
};